package org.huang.sample15

import org.huang.sample15.bo.Dept
import org.huang.sample15.dao.DeptXmlMapper
import org.huang.sample15.service.DemoService
import org.junit.runner.RunWith
import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import spock.lang.Specification

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ActiveProfiles("test")
class AppServerTest extends Specification {

    @SpringBean
    DeptXmlMapper deptXmlMapper = Mock()

//    @TestConfiguration
//    static class MockConfig {
//        def detachedMockFactory = new DetachedMockFactory()
//        @Bean
//        DeptXmlMapper deptXmlMapper() {
//            return detachedMockFactory.Stub(DeptXmlMapper)
//        }
//    }

    @Autowired
    private DemoService demoService;

    def "deptXmlMapper selectAll"(){
        expect : demoService.getDeptById(1) == [new Dept(id:1, name:'技术部', code:'0010')]
    }

    def "test method getDeptById"(){
        given:
        deptXmlMapper.selectByPrimaryKey(_) >> null
        when:
        Dept result = demoService.getDeptById(1)
        then:
        result == new Dept(id:1, name:'技术部', code:'0010')
    }
}